﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Budget.DataAccess.Services;
using Budget.DataAccess;
using Budget.DataAccess.Repository;
using Budget.HelperClasses;

namespace Budget.WCF
{
	// NOTE: If you change the class name "SpendingService" here, you must also update the reference to "SpendingService" in App.config.
	public class SpendingService : ISpendingService
	{
		private ISpendingDataService svc;

		#region ISpendingService Members

		public bool SpendingsExistForMonthYear( int month, int year )
		{
			if ( svc == null )
			{
				svc = new SpendingDataService( new BudgetRepository<Spending>( new BudgetDataContext() ) );
			}

			return svc.SpendingsExistForMonthYear( month, year );
		}

		public bool SpendingsExistForMonth( int mthId )
		{
			if ( svc == null )
			{
				svc = new SpendingDataService( new BudgetRepository<Spending>( new BudgetDataContext() ) );
			}

			return svc.SpendingsExistForMonth( mthId );
		}

		public IList<MonthSummary> GetMonthCategorySummary( DateTime analysisDate )
		{
			if ( svc == null )
			{
				BudgetDataContext context = new BudgetDataContext(); // use a common DataContext
				BudgetRepository<Spending> sRepository = new BudgetRepository<Spending>( context);
				BudgetRepository<MthCategory> mcRepository = new BudgetRepository<MthCategory>( context );
			
				svc = new SpendingDataService( sRepository, mcRepository );
			}

			IList<MonthSummary> list = svc.GetMonthCategorySummary( analysisDate );

			return list;
		}

		public IList<Spending> GetSpendingsByMonthAndYear( int month, int year )
		{
			if ( svc == null )
			{
				BudgetDataContext context = new BudgetDataContext(); // use a common DataContext
				BudgetRepository<Spending> sRepository = new BudgetRepository<Spending>( context );

				svc = new SpendingDataService( sRepository );
			}

			return svc.GetSpendingsByMonthAndYear( month, year );
		}

		public IList<Spending> GetSpendingsByMonth( int mthId )
		{
			if ( svc == null )
			{
				BudgetDataContext context = new BudgetDataContext(); // use a common DataContext
				BudgetRepository<Spending> sRepository = new BudgetRepository<Spending>( context );

				svc = new SpendingDataService( sRepository );
			}

			return svc.GetSpendingsByMonth( mthId );
		}

		public IList<Spending> GetSpendingsByMthCategoryId( int mthCategoryId )
		{
			if ( svc == null )
			{
				BudgetDataContext context = new BudgetDataContext(); // use a common DataContext
				BudgetRepository<Spending> sRepository = new BudgetRepository<Spending>( context );

				svc = new SpendingDataService( sRepository );
			}

			return svc.GetSpendingsByMthCategoryId( mthCategoryId );
		}


		public void Save( Spending spending )
		{
			if ( svc == null )
			{
				BudgetDataContext context = new BudgetDataContext(); // use a common DataContext
				BudgetRepository<Spending> sRepository = new BudgetRepository<Spending>( context );

				svc = new SpendingDataService( sRepository );
			}

			svc.Save( spending );
		}

		#endregion
	}
}
